var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../utils/user.js');
var app = getApp();

Page({
  data: {
    StaticUrl: api.StaticUrl,
    userInfo: {
      nickName: '点击登录',
      avatarUrl: '/static/images/default-avatar.png',
      isAuth: false,
    },
    hasLogin: false,

    service: [{
      id: 'serviceOrder',
      name: '充值记录',
      icon: 'wx/cg_ucenter/record.png',
      type: 'link',
      url: '/pages/ucenter/orderList/index'
    }],
    content:"<p style='text'>工程管理硕士<br/> 软件工程师<br/>软件工程造价师<br/>房地产估价师<br/>土地估价师 <br/>矿权评估师 <br/>价格鉴证师 <br/>物业管理师 <br/>房地产经纪人 <br/>不动产登记代理人</p>"
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady: function () {

  },
  onShow: function () {

    //获取用户的登录信息
    if (app.globalData.hasLogin) {


      try {
        user.getUserInfoServer().then(res => {
          let userInfo = user.getLoginUserInfo();
          console.log(userInfo)
          userInfo = Object.assign(this.data.userInfo, userInfo);

          if (userInfo.nickName == '') {
            userInfo.nickName = '用户昵称'
          }
          if (userInfo.avatarUrl == '') {
            userInfo.avatarUrl = '/static/images/default-avatar.png'
          }
          this.setData({
            userInfo: userInfo,
            hasLogin: true,
          });
          console.log(this.data.userInfo);
        }).catch(err => {

        });



      } catch (e) {
        console.log(e);
      }


    } else {
      /** 
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });**/
    }



  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  },

  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    wx.hideNavigationBarLoading() //完成停止加载
    //刷新用户
    user.getUserInfoServer().then(res => {
      console.log("ss");
    }).catch(err => {

    });
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  nickClick: function () {
    console.log(111);
    if (!this.data.hasLogin) {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    } else {
      wx.navigateTo({
        url: "/pages/ucenter/edit/index"
      });
    }
  },


  charge: function () {
    if (!this.data.hasLogin) {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    } else {
      wx.navigateTo({
        url: "/pages/ucenter/charge/index"
      });
    }
  },




  handleService: function (e) {
    console.log(e)

    let index = e.currentTarget.dataset.index;
    console.log(index)
    let url = this.data.service[index].url;
    console.log(url)
    wx.navigateTo({
      url: url,
    })


  },


  handleLogout: function () {
    wx.showModal({
      title: '',
      confirmColor: '#296DBA',
      content: '退出登录？',
      success: function (res) {
        if (!res.confirm) {
          return;
        }
        app.globalData.hasLogin = false;
        wx.removeStorageSync('token');
        wx.removeStorageSync('userInfo');
        wx.reLaunch({
          url: '/pages/index/index'
        });
      }
    })
  },


  contactTel() {
    console.log("contactTel");
    wx.makePhoneCall({
      phoneNumber: '13888217952' //仅为示例，并非真实的电话号码
    })
  },


})